//
//  QAExpertsTableViewCell.m
//  CYWFrame
//
//  Created by Macx on 2017/9/27.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "QAExpertsTableViewCell.h"
#import "User.h"
#import "PushPerHome.h"

@implementation QAExpertsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.nickName.font = kUHSystemFontWithSize(14);
    self.intro.font = kUHSystemFontWithSize(14);
    self.fans.font = kUHSystemFontWithSize(12);
    self.reply.font = kUHSystemFontWithSize(12);
    self.chat.titleLabel.font = kUHSystemFontWithSize(13);
    
    self.headPic.frame = CGRectMake(10, 10, SW(9), SW(9));
    self.headPic.layer.cornerRadius = SW(18);
    self.headPic.layer.masksToBounds = YES;
    
    self.role.frame = CGRectMake(self.headPic.x+self.headPic.width/3*2, self.headPic.y+self.headPic.height/3*2, self.headPic.width/3, self.headPic.width/3);
    
    self.focus.titleLabel.font = kUHSystemFontWithSize(13);
    [self.focus setTitleColor:kWhiteColor forState:UIControlStateNormal];
    CGSize size3 = [LZCTools textSizeWithText:@"未关注" font:kUHSystemFontWithSize(13)];
    //    CGFloat focusWidth = self.headPic.height/2+5+size3.width;
    CGFloat focusWidth = 10+size3.width+size3.height;
    self.focus.frame = CGRectMake(SCREEN_WIDTH-10-focusWidth, 0, focusWidth, size3.height+8);
    self.focus.centerY = self.headPic.centerY;
    
    self.line.backgroundColor = UISeparatedColorRGB;
    
}

- (void)setModel:(User *)model {
    if (_model != model) {
        _model = model;
        
        [self.headPic sd_setImageWithURL:[NSURL URLWithString:model.face] placeholderImage:IMAGE(@"placeHolder")];
        
        NSDictionary *user = @{@"is_blocked":[NSString stringWithFormat:@"%ld", model.is_blocked],
                               @"user_id":[NSString stringWithFormat:@"%ld", model.user_id],
                               @"face":model.face,
                               @"nickname":model.nickname,
                               @"role":[NSString stringWithFormat:@"%ld", model.role],
                               @"sex":[NSString stringWithFormat:@"%ld", model.sex],
                               @"is_like":[NSString stringWithFormat:@"%ld", model.is_like],
                               @"pro_intro":model.pro_intro};
        
        [self.headPic addActionWithblock:^{
            
            [PushPerHome pushPerHomeWithUserInfo:user];
        }];
        
        self.nickName.text = model.nickname;
        self.intro.text = model.intro;
        self.fans.text = [NSString stringWithFormat:@"%ld粉丝", model.followers_count];
        self.reply.text = [NSString stringWithFormat:@"%ld回答", model.answers_count];
        
        if (model.role == 1) {
            self.role.image = IMAGE(@"");
        }
        else if (model.role == 2) {
            self.role.image = IMAGE(@"icon_add_v");
        }
        else if (model.role == 3) {
            self.role.image = IMAGE(@"icon_add_v_expert");
        }
        
        
        if ([[GetUserDefaule(USERINFO) valueForKey:@"user_id"] isEqualToString:[NSString stringWithFormat:@"%ld", model.user_id]]) {
            self.focus.hidden = YES;
        }
        else
            self.focus.hidden = NO;
        
        if (model.is_like == 1) {
            [self.focus setBackgroundImage:IMAGE(@"btn_concerned_bg") forState:UIControlStateNormal];
            [self.focus setImage:[IMAGE(@"btn_ed_concern") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [self.focus setTitle:@"已关注" forState:UIControlStateNormal];
        }
        else {
            [self.focus setBackgroundImage:IMAGE(@"btn_unconcern_bg") forState:UIControlStateNormal];
            [self.focus setImage:[IMAGE(@"btn_un_concern") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [self.focus setTitle:@"  关注" forState:UIControlStateNormal];
        }
        

        // 布局
        CGSize size1 = [LZCTools textSizeWithText:self.nickName.text font:kUHSystemFontWithSize(14)];
        CGSize size2 = [LZCTools textSizeWithText:@"xx" font:kUHSystemFontWithSize(13)];
        CGSize size3 = [LZCTools textSizeWithText:self.fans.text font:self.fans.font];
        CGSize size4 = [LZCTools textSizeWithText:self.reply.text font:self.reply.font];
        CGSize moodSize = [self.intro.text sizeWithFont:kUHSystemFontWithSize(13) constrainedToSize:CGSizeMake(SCREEN_WIDTH-self.headPic.x*2, size2.height*10) lineBreakMode:NSLineBreakByWordWrapping];
        
        self.nickName.frame = CGRectMake(self.headPic.x+self.headPic.width+10, 10, size1.width, size1.height);
        self.intro.frame = CGRectMake(self.headPic.x, self.headPic.y+self.headPic.height+10, SCREEN_WIDTH-self.headPic.x*2, moodSize.height);
        CGFloat fanY = self.intro.y+self.intro.height+10;
        self.fans.frame = CGRectMake(self.headPic.x, fanY, size3.width, size3.height);
        self.reply.frame = CGRectMake(self.fans.x+self.fans.width+10, fanY, size4.width, size4.height);
        
        self.chat.frame = CGRectMake(self.focus.x, 0, self.focus.width, self.focus.height);
        self.chat.centerY = self.fans.centerY;
        
        self.line.frame = CGRectMake(0, self.fans.y+self.fans.height+10, SCREEN_WIDTH, 10);
        
        
        self.height = self.line.y+self.line.height;
    }
}

- (CGFloat)cellHeight {
    
    //    return self.sepView.y+self.sepView.height;
    return self.height;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
